package com.project2.controller;

import com.project2.anno.SaveLog;
import com.project2.bean.CommonData;
import com.project2.bean.ResultVo;
import com.project2.bean.bo.WxOrderCommentBO;
import com.project2.bean.bo.WxOrderMBO;
import com.project2.bean.bo.WxOrderSubmitBO;
import com.project2.bean.po.MarketOrderGoods;
import com.project2.bean.po.MarketUser;
import com.project2.bean.vo.WxOrderDetailVO;
import com.project2.bean.vo.WxOrderSubmitVO;
import com.project2.execption.Code;
import com.project2.execption.SystemException;
import com.project2.service.WxOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: cc
 * @Date: 2022-08-14 17:11
 * @Description: 订单模块
 */


@RestController
@RequestMapping("wx/order")
public class WXOrderController {
    @Autowired
    WxOrderService wxOrderService;


    /**
     * 直接付款
     * @param orderSubmitBO
     * @return
     */
    @SaveLog(value = "用户已付款")
    @PostMapping("submit")
    public ResultVo orderSubmit(@RequestBody WxOrderSubmitBO orderSubmitBO, HttpServletRequest request){

        //需要特殊处理 才能出现  处理cancel逻辑
         WxOrderSubmitVO data = wxOrderService.submitOrder(orderSubmitBO,request);

        return ResultVo.ok(data);
    }


    /**
     * 显示订单
     * @param showType
     * @param page
     * @param limit
     * @return
     */

    @GetMapping("list")
    public ResultVo orderList(Integer showType,Integer page,Integer limit,HttpServletRequest request){


      CommonData data =  wxOrderService.orderList(showType,page,limit,request);

    return ResultVo.ok(data);
    }


    /**
     * 退款
     * @return
     */
    @SaveLog(value = "小程序用户申请退款")
    @PostMapping("refund")
    public ResultVo orderRefund(@RequestBody WxOrderMBO wxOrderMBO){

        int refund = wxOrderService.refundOrder(wxOrderMBO);

        if (refund != 1){
            return ResultVo.wrong("申请退款失败");
        }

            return ResultVo.ok();
    }


    /**
     * 删除订单
     * @return
     */

    @PostMapping("delete")
    public ResultVo orderDelete(@RequestBody WxOrderMBO wxOrderMBO){

        int delete = wxOrderService.deleteOrder(wxOrderMBO);

        if (delete != 1){
            return ResultVo.wrong("订单删除异常");
        }
        return ResultVo.ok();

    }


    /**
     * 确认收货
     */

    @PostMapping("confirm")
    public ResultVo orderConfirm(@RequestBody WxOrderMBO wxOrderConfirmBO){

      int confirm = wxOrderService.confirmOrder(wxOrderConfirmBO);

      if (confirm != 1){
          return ResultVo.wrong("确认收货失败");
      }
       return ResultVo.ok();
    }


    /**
     * 预付款
     */

    @PostMapping("prepay")
    public ResultVo orderRepay(@RequestBody WxOrderMBO wxOrderMBO){


      int num = wxOrderService.payOrder(wxOrderMBO.getOrderId());
        return ResultVo.ok();
    }


    /**
     * 去评价商品
     */

    @GetMapping("goods")
    public ResultVo orderInfoGoods(Integer orderId,Integer goodsId){

        List<MarketOrderGoods> date = wxOrderService.goodsInfo(orderId,goodsId);

       return ResultVo.ok(date);

    }


    /**
     * 显示订单详情（点击申请售后）
     */

    @GetMapping("detail")
    public ResultVo orderDetail(Integer orderId){

        WxOrderDetailVO date = wxOrderService.orderDetail(orderId);

        return ResultVo.ok(date);
    }


    /**
     * 提交评论
     */

    @PostMapping("comment")
    public ResultVo orderAddComment(@RequestBody WxOrderCommentBO wxOrderCommentBO,
                                    HttpServletRequest request){

        String header = request.getHeader("X-CskaoyanMarket-Token");
        MarketUser userInfo = (MarketUser) request.getSession().getAttribute(header);


        int comment =  wxOrderService.addComment(wxOrderCommentBO,userInfo.getId());

      if (comment != 1){
          return ResultVo.wrong("提交评论失败");
      }

        return ResultVo.ok();
    }



    @PostMapping("cancel")
    public ResultVo orderCancel(@RequestBody WxOrderMBO wxOrderMBO){

        try {
            int i = 1/0;
        } catch (Exception e) {
            throw new SystemException(Code.WX_ORDER_CANCEL_ERR,e);
            // throw new SystemException(300,"异常");
        }

        int cancel = wxOrderService.cancelOrder(wxOrderMBO);

        if (cancel != 1){
            return ResultVo.wrong("取消订单失败");
        }
         return ResultVo.ok();
    }

}
